package com.Shoping.controller;

import com.Shoping.util.DbUtils;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(value = "/shoppingNum.do")
public class ShoppingNumController extends HttpServlet {

    Connection connection = DbUtils.getConnection();
    PreparedStatement ps;
    ResultSet rs;
    Integer cmPrice=0;//商品单价
    Integer cmId=0;//商品id

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer flag=Integer.valueOf(req.getParameter("flag"));//获取用户的操作，+为‘1’，-为‘0’;
        Integer cmsId=Integer.valueOf(req.getParameter("cmsId"));

        /**
         * 判断flag的值
         */
        if(flag == 1){
            /**
             *获取商品id
             */
            getCmId(cmsId);
            /**
             * 获取商品单价
             */
            getPrice(cmsId);
            /**
             * 更新价格和数量
             */
            try {
                ps=connection.prepareStatement("update commoditys set cms_number=cms_number+1,cms_subtotal=cms_number *"+cmPrice+" where cms_id = "+cmsId);
                ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingNumController-----------------数量+1错误!");
            }finally {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }else if (flag == 0){
            /**
             *获取商品id
             */
            getCmId(cmsId);
            /**
             * 获取商品单价
             */
            getPrice(cmsId);

            /**
             * 更改数量和价格
             */
            try {
                ps=connection.prepareStatement("update commoditys set cms_number=cms_number-1,cms_subtotal=cms_subtotal-"+cmPrice+"  where cms_id = "+cmsId);
                ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingNumController-----------------数量-1错误!");
            }finally {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }else{

            try {
                ps=connection.prepareStatement("delete from commoditys where cms_id = "+cmsId);
                ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingNumController-----------------删除错误!");
            }finally {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        Map<String, String> map = new HashMap<>();
        map.put("flag", "1");
        String jsonStr = JSON.toJSONString(map);
        resp.getWriter().write(jsonStr);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * 获取商品单价
     * @param cmsId
     * @return
     */
    private void getPrice(Integer cmsId){
        try {
            ps=connection.prepareStatement("select cm_price from commodity where cm_id = "+cmId);
            rs=ps.executeQuery();
            if (rs.next()){
                cmPrice=rs.getInt("cm_price");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("ShoppingNumController-----------------获取商品价格错误!");
        }finally {
            try {
                ps.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取商品id
     * @param cmsId
     * @return
     */
    private void getCmId(Integer cmsId){
        try {
            ps=connection.prepareStatement("select cm_id from commoditys where cms_id = "+cmsId);
            rs=ps.executeQuery();
            if(rs.next()){
                cmId=rs.getInt("cm_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("ShoppingNumController-----------------获取商品id错误!");
        }finally {
            try {
                ps.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
